<?php
App::uses('HttpSocket', 'Network/Http');

class GeocoderComponent extends Component {
    
    /**
     * Geocodes an address.
     *
     * @param string $address
     * @param array $parameters
     * @return object
     * @todo Determine what to do if response status is an error
     */
    public function geocode($address, $parameters = array()) {
        
        $parameters['address'] = $address;
        $parameters['sensor'] = 'false';
        
        $url = 'http://maps.googleapis.com/maps/api/geocode/json';
        
        $http = new HttpSocket();
        
        $response = $http->get($url, $parameters);
        $response = json_decode($response);
        
        //return $response->results;
        $lat=$response->results[0]->geometry->location->lat;
        $long=$response->results[0]->geometry->location->long;
        $key = 'AhOqe3nZwtht6ytH1npIOIKRV2ppLtyYDwMjoa7ZMUrkWk8rEClXWnBUYwihqGb6';
        $prepAddr = str_replace(' ', '+', $address);
        //$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
        $geocode = file_get_contents('https://maps.googleapis.com/maps/api/place/nearbysearch/json?location='.$lat.','.$long.'&radius=500&types=train_station&rankby=distance&sensor=false&key='. $key);
        //return $geocode;
       
            $ts=0;
            $bs=0;
            $ss=0;
            $limit=3; 
        $output = json_decode($geocode);
        for ($i=0;$i<$output->length;$i++){
            for ($j=0 ;$j<$output[$i]->types->length;$j++)
            {
                 if ($results[i]->types[j]=='train_station' && $ts<$limit){
                        $TrainStations= $TrainStations . $results[i]->name . ' - ' . getDistanceFromLatLonInKm($lat,$long,$results[$i]->geometry->location->lat,$results[i]->geometry->location->lng) . ' km <br/>';
                        $ts++;
                    }
                    else if($results[$i]->types[$j]=='bus_station' && $bs<$limit){
                        $BusStops=$BusStops . $results[$i]->name . ' - ' . getDistanceFromLatLonInKm($lat,$long,$results[$i]->geometry->location->lat,$results[i]->geometry->location->lng) . ' km <br/>';
                        $bs++;
                    }
                    else if($results[$i]->types[$j]=='subway_station' && $ss<$limit){
                        $SubWays=$SubWays+$results[i]->name . ' - ' . getDistanceFromLatLonInKm($lat,$long,$results[$i]->geometry->location->lat,$results[i]->geometry->location->lng) . ' km <br/>';
                        $ss++;
                    }
            }
    
        }
        $final['ts']=$TrainStations;
        $final['ss']=$SubWays;
        $final['bs']=$BusStops;
       return $final;
    }
    function getDistanceFromLatLonInKm($lat1,$lon1,$lat2,$lon2) {
        $R = 6371; // Radius of the earth in km
        $dLat = deg2rad($lat2-$lat1);  // deg2rad below
        $dLon = deg2rad($lon2-$lon1); 
        $a = 
            sin($dLat/2) * sin($dLat/2) +
            cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * 
            sin($dLon/2) * sin($dLon/2)
        ; 
        $c = 2 * atan2(sqrt($a), sqrt(1-$a)); 
        $d = $R * $c; // Distance in km
        return round(d*100)/100;
    }
    function deg2rad($deg) {
        return $deg * (pi()/180);
    }
}